//
//  CarDetailListCell.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/4.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "CarDetailListCell.h"
#import "UILabel+BasicAttributes.h"
#import "NSString+StringSize.h"

@interface CarDetailListCell ()
@property (nonatomic,strong)UILabel *platLabel;
@property (nonatomic,strong)UILabel *VINLabel;
@property (nonatomic,strong)UILabel *timeLabel;
@property (nonatomic,strong)UILabel *areaLabel;
@property (nonatomic,strong)UILabel *speedLabel;
@property (nonatomic,strong)UILabel *locationLabel;
@property (nonatomic,strong)UILabel *ownerLabel;
@property (nonatomic,strong)UILabel *mileageLabel;
@property (nonatomic,strong)UILabel *comStatusLabel;
@property (nonatomic,strong)UILabel *currentState;
@end

@implementation CarDetailListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setUpUI];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor colorWithRed:0.88f green:0.88f blue:0.88f alpha:1.00f];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    [self.contentView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.contentView);
        make.height.mas_equalTo(29);
    }];
    
    _platLabel = [UILabel LabelWithFont:NormalFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:right];
    _platLabel.text = @"粤B88888";
    [headerView addSubview:_platLabel];
    [_platLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.left.mas_equalTo(self.contentView.mas_left).offset(kWidthOfPt(25));
    }];
    
    _VINLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:center];
    _VINLabel.text = @"VIN:123456789000";
    [headerView addSubview:_VINLabel];
    [_VINLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(headerView);
    }];
    
    _timeLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:right];
    _timeLabel.text = @"11/28 15:30:01";
    [headerView addSubview:_timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.right.mas_equalTo(headerView.mas_right).offset(kWidthOfPt(-25));
    }];
    
    UILabel *titleAreaLab = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:1.00f] andTextAlignment:left];
    titleAreaLab.text = @"归属地区";
    [self.contentView addSubview:titleAreaLab];
    [titleAreaLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.125);
    }];
    
    _areaLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:left];
    _areaLabel.text = @"深圳";
    [self.contentView addSubview:_areaLabel];
    [_areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleAreaLab.mas_right).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.125);
    }];
    
    UILabel *comStatusTitleLab = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:1.00f] andTextAlignment:left];
    comStatusTitleLab.text = @"通讯状态";
    [self.contentView addSubview:comStatusTitleLab];
    [comStatusTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.375);
    }];
    
    _comStatusLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:left];
    _comStatusLabel.text = @"通讯正常";
    [self.contentView addSubview:_comStatusLabel];
    [_comStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleAreaLab.mas_right).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.375);
    }];
    
    
    
    
    
    UILabel *titleSpeedLab = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:1.00f] andTextAlignment:left];
    titleSpeedLab.text = @"当前速度";
    [self.contentView addSubview:titleSpeedLab];
    [titleSpeedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.625);
    }];
    
    
    
    _speedLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:left];
    _speedLabel.text = @"50km/h";
    [self.contentView addSubview:_speedLabel];
    [_speedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleSpeedLab.mas_right).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.625);
    }];
    
    UILabel *titleLocationLab = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:1.00f] andTextAlignment:left];
    titleLocationLab.text = @"当前位置";
    [self.contentView addSubview:titleLocationLab];
    [titleLocationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.875);
    }];
    
    
    
    
    
    
    
    _locationLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:left];
    _locationLabel.numberOfLines = 0;
    _locationLabel.text = @"深圳市坪山新区锦绣中路19号美讯科技园";
    [self.contentView addSubview:_locationLabel];
    [_locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleLocationLab.mas_right).offset(kWidthOfPt(45));
        make.top.mas_equalTo(titleLocationLab.mas_top);
        make.width.mas_equalTo(kScreenWidth - 90);
    }];
    
    
    UILabel *titleOwnerLab = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:1.00f] andTextAlignment:left];
    titleOwnerLab.text = @"归属业主";
    [self.contentView addSubview:titleOwnerLab];
    [titleOwnerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_centerX).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.125);
    }];
    
    _ownerLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:left];
    _ownerLabel.text = @"新沃总公司";
    [self.contentView addSubview:_ownerLabel];
    [_ownerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleOwnerLab.mas_right).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.125);
    }];
    
    UILabel *currentStateTitleLab = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:1.00f] andTextAlignment:left];
    currentStateTitleLab.text = @"行驶情况";
    [self.contentView addSubview:currentStateTitleLab];
    [currentStateTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_centerX).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.375);
    }];
    
    _currentState = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:left];
    _currentState.text = @"行驶";
    [self.contentView addSubview:_currentState];
    [_currentState mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleOwnerLab.mas_right).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.375);
    }];
    
    UILabel *titleMileageLab = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:1.00f] andTextAlignment:left];
    titleMileageLab.text = @"今日里程";
    [self.contentView addSubview:titleMileageLab];
    [titleMileageLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_centerX).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.625);
    }];
    
    _mileageLabel = [UILabel LabelWithFont:SmallFontSize andColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] andTextAlignment:left];
    _mileageLabel.text = @"200km";
    [self.contentView addSubview:_mileageLabel];
    [_mileageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleOwnerLab.mas_right).offset(kWidthOfPt(45));
        make.centerY.mas_equalTo(headerView.mas_bottom).offset((149-29)*0.625);
    }];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _platLabel.text = _model.plateNum;
    _VINLabel.text = [NSString stringWithFormat:@"VIN:%@",_model.frameNum];
//    _VINLabel.text = @"VIN:99999999999999999";
    _areaLabel.text = _model.areaName;
    _speedLabel.text = [NSString stringWithFormat:@"%.2lfkm/h",[_model.speed doubleValue]];
    _locationLabel.text = [_model.position stringByReplacingOccurrencesOfString:@";" withString:@""];
    _ownerLabel.text = _model.deptName;
    _mileageLabel.text = [NSString stringWithFormat:@"%.2lfkm",[_model.todayMileage doubleValue]];
    _timeLabel.text = [self clipStringWithString:_model.time];
    _comStatusLabel.text = _model.comStatus;
    _currentState.text = _model.currentState;
    if ([_model.currentState isEqualToString:@"行驶"]) {
        _currentState.textColor = [UIColor colorWithRed:0.28f green:0.70f blue:0.08f alpha:1.00f];
    }else if ([_model.currentState isEqualToString:@"停车"]) {
        _currentState.textColor = [UIColor colorWithRed:0.15f green:0.53f blue:0.91f alpha:1.00f];
    }else if ([_model.currentState isEqualToString:@"熄火"]) {
        _currentState.textColor = [UIColor colorWithRed:0.92f green:0.41f blue:0.64f alpha:1.00f];
    }else{
        if (!_model.currentState.length) {
            _currentState.text = @"--";
        }
         _currentState.textColor = [UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f];
    }
    
    
    
    
}
//171211172404 2018-01-11 11:11:39
- (NSString *)clipStringWithString:(NSString *)string
{
    if (string.length) {
        NSMutableString *mString = [NSMutableString stringWithString:string];
        NSString *str2 = [mString stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSString *str4 = [str3 stringByReplacingOccurrencesOfString:@":" withString:@""];
        [mString deleteCharactersInRange:NSMakeRange(0, 2)];
        if (str3.length<12) {
            return @"";
        }else{
            NSString *mounthStr = [str4 substringWithRange:NSMakeRange(4, 2)];
            NSString *dateStr = [str4 substringWithRange:NSMakeRange(6, 2)];
            NSString *hourStr = [str4 substringWithRange:NSMakeRange(8, 2)];
            NSString *minuteStr = [str4 substringWithRange:NSMakeRange(10, 2)];
            NSString *secondStr = [str4 substringWithRange:NSMakeRange(12, 2)];
            return [NSString stringWithFormat:@"%@/%@ %@:%@:%@",mounthStr,dateStr,hourStr,minuteStr,secondStr];
        }
    }else{
        return @"";
    }
}


+ (CGFloat)tableView:(UITableView *)tableView rowHeightForObject:(id)object
{
    CGFloat statusLabelWidth = kScreenWidth - 90;
    // 字符串分类提供方法，计算字符串的高度
    CGSize statusLabelSize =[object sizeWithLabelWidth:statusLabelWidth font:[UIFont systemFontOfSize:SmallFontSize]];
    return statusLabelSize.height;
}




@end
